//alert('Inside content script');

/*
function fixcsspath(rules, folder){
	        var path_prefix = chrome.extension.getURL('');
	        var lookfor = 'url(';
	        var ss = document.styleSheets;
	
	        for (var j=0; j<rules.length; j++) {
	            var b = rules[j].style['background-image'];
	            var s;
	            if (b && (s = b.indexOf(lookfor)) >= 0 ){
	                s = s + lookfor.length;
	                rules[j].style['background-image'] = b.replace(b.substr(s,b.indexOf(folder)-s), path_prefix);
	            }   
	        }
	    };
*/
  
chrome.extension.onConnect.addListener(function(port) {
    //alert('inside connect listener, port.name = ' + port.name);
    port.onMessage.addListener(function getResp(msg) {
        //alert('inside message listener');
        
        //alert(msg.urls);
        if (msg.urls){
            //update path for image in css
     	
 	
 
            //build the dialog
            //alert('build the dialog');
            var containerDiv = $("<div id='dialog-container' style='display:none'></div>");
            var formDiv = $("<div class='dialog-form' title='Open All URL'>Chose the URLs you want to open:</div>");
            formDiv.appendTo(containerDiv);
 	
 	
            var contentDiv = $('<div>');
            var ul = $('<ul style="list-style-type: none;padding-left: 0px;font-size:12px;">');
 
            ul.appendTo(contentDiv);
            contentDiv.appendTo(formDiv);
 
            for (var i=0;i<=msg.urls.length-1;i++){
                var li = $('<li>');
                var input = $('<input name="option" type="checkbox"> checked');
                var a = $('<a href="" style="color:dodgerblue;margin-left: 25px; white-space: nowrap;">');
                a.text(msg.urls[i]);
                a.attr('href',msg.urls[i]);
                input.attr('value',msg.urls[i]);
                a.appendTo(input);
                input.appendTo(li);
                li.appendTo(ul);
            }
 	
 	
            containerDiv.appendTo('body');
 
            //containerDiv.dialog({autoOpen:true,modal:true});
            //alert('enable: ' + containerDiv.dialog('enable'));
            containerDiv.dialog({
                autoOpen:true,
                modal:true,
                resizable:true,
                title:'Open All URL',
                width:600,
                buttons:{
                    "Open":function(){
                      
                        port.postMessage({
                            urls: $(this).find(':checked').map(function(){
                                return $(this).val();
                            }).toArray()
                        });
                    },
                    "Cancel":function(){
                        containerDiv.dialog('destroy');
                    }
                }
            });
            $('.ui-dialog-buttonset button').addClass('ui-button').addClass('ui-widget').addClass('ui-state-default').addClass('ui-corner-allt').addClass('ui-button-text-only');
 		
        }
    });
    //alert('add listner on content script');
});